﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Itegracion.Framework.Forms
{
    public partial class ucEdicion : UserControl
    {
        protected string ensamblado;
        protected string clase;
        public ucEdicion()
        {
            InitializeComponent();
        }

        #region Propiedades
        private Dictionary<int, string> config;
        public Dictionary<int, string> Configuration
        {
            get { return config; }
            set { config = value; }
        }
        #endregion

        #region Metodos
        public virtual void OnLoadData()
        {
            MessageBox.Show(string.Format("Edicion: OnLoadData()"));
        }
        public virtual void OnNew()
        {
            MessageBox.Show(string.Format("Edicion: OnNew({0})", this));
        }
        public virtual void OnEdit(string id, DataGridViewCell cell)
        {
            MessageBox.Show(string.Format("Edicion: OnEdit({0}, {1})", id, cell));
        }
        public virtual void OnProperty(string id, DataGridViewCell cell)
        {
            MessageBox.Show(string.Format("Edicion: OnProperty({0}, {1})", id, cell));
        }
        public virtual void OnPrint()
        {
            MessageBox.Show(string.Format("Edicion: OnPrint()"));
        }
        public virtual bool AreDataValid(ModoMantenimiento modo)
        {
            MessageBox.Show(string.Format("Edicion: bool AreDataValid(ModoMantenimiento modo)"));
            return true;
        }
        public virtual void OnSave(object id, ModoMantenimiento modo)
        {
            MessageBox.Show(string.Format("Edicion: OnSave(object id, ModoMantenimiento modo)"));
        }
        public virtual void OnAddItem()
        {
            MessageBox.Show(string.Format("Edicion: OnAddItem()"));
        }
        public virtual void OnRemoveItem()
        {
            MessageBox.Show(string.Format("Edicion: OnRemoveItem()"));
        }
        public virtual bool OnDelete(string id, DataGridViewCell cell)
        {
            Element rpta = new Element();
            _("Eliminar", cell)
                .Out(rpta);
            return true;
        }
        public virtual bool Activo() {
            return true;
        }
        #endregion

        #region helpers
        protected bool Error(Control control, string message)
        {
            tt.SetToolTip(control, string.Format(message, control.Name, control.Tag));
            ep.SetError(control, string.Format(message, control.Name, control.Tag));
            return false;
        }
        protected Helper _(string assembly, string type, string method, params object[] controls)
        {
            return Helper.Instance.In(assembly, type, method, controls);
        }
        protected Helper _(string method, params object[] controls)
        {
            return Helper.Instance.In(ensamblado, clase, method, controls);
        }

        public void Clear() {
            Clear(this);
        }

        private void Clear(Control container) {
            foreach (Control control in container.Controls)
            {
                if (control is TextBox)
                    (control as TextBox).Clear();
                if (control is ComboBox)
                    (control as ComboBox).SelectedIndex = 0;
                if (control is DateTimePicker)
                    (control as DateTimePicker).Value = DateTime.Today;
                if (control is Panel)
                    Clear(control);
                if (control is TabControl)
                    foreach (TabPage item in (control as TabControl).TabPages)
                    {
                        Clear(item);
                    }
            }
        }
        public void Enable() {
            SetState(this, true);
        }
        public void Disable()
        {
            SetState(this, false);
        }
        private void SetState(Control container, bool enable)
        {
            foreach (Control control in container.Controls)
            {
                if (control is TextBox)
                    (control as TextBox).Enabled = enable;
                if (control is ComboBox)
                    (control as ComboBox).Enabled = !((control as ComboBox).Tag + "").Contains("ReadOnly") && enable;
                if (control is DateTimePicker)
                    (control as DateTimePicker).Enabled = enable;
                if (control is DataGridView)
                    (control as DataGridView).Enabled = enable;
                if (control is Panel)
                    SetState(control as Panel, enable);
                if (control is TabControl)
                    foreach (TabPage item in (control as TabControl).TabPages)
                    {
                        SetState(item, enable);
                    }
            }
        }
        #endregion
    }
}
